<template>
  <div class="shopcar_container">
    <div class="good_list">
      <!-- 商品列表区域 -->
      <div class="mui-card" v-for="(item,i) in goodlist" :key="item.id">
        <div class="mui-card-content">
          <div class="mui-card-content-inner">
            <mt-switch
              v-model="$store.getters.getSelectedStatus[item.id]"
              @change="selectedChanged(item.id,$store.getters.getSelectedStatus[item.id])"
            ></mt-switch>
            <img :src="item.thumb_path" alt>
            <div class="infos">
              <h3>{{item.title}}</h3>
              <div class="li">
                <span class="price">￥{{item.sell_price}}</span>
                <shopcar_number
                  class="number_box"
                  :initCount="$store.getters.getInitCount[item.id]"
                  :goodId="item.id"
                ></shopcar_number>
                <a href="#" @click.prevent="remove(item.id,i)">删除</a>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!-- 商品结算区域 -->
      <div class="mui-card">
        <div class="mui-card-content">
          <div class="mui-card-content-inner jiesuan">
            <div class="left">
              <p>总计（不含运费）</p>
              <p>
                已选商品
                <span class="red">{{$store.getters.getGoodsCountAndAmount.count}}</span>件，总价：
                <span class="red">￥{{$store.getters.getGoodsCountAndAmount.amount}}</span>
              </p>
            </div>
            <mt-button type="danger" size="small">去结算</mt-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import shopcar_number from "../subcomponents/shopcar_number.vue";
export default {
  data() {
    return {
      goodlist: []
    };
  },
  created() {
    this.getGoodlist();
  },
  methods: {
    getGoodlist() {
      //获取购物车中的所有 商品id
      var idArr = [];
      this.$store.state.carInfo.forEach(element => {
        idArr.push(element.id);
      });
      //如果购物车中没有商品， 直接退出，防止报错
      if (idArr.length <= 0) {
        return;
      }
      this.$http
        .get("api/goods/getshopcarlist/" + idArr.join(","))
        .then(result => {
          if (result.body.status == 0) {
            this.goodlist = result.body.message;
          }
        });
    },
    remove(id, index) {
      //删除购物车中的商品:id用于删除本地存放的carInfo数据，index用于删除列表中的数据
      // 1.先删除shopcar组件中goodlist的数据
      this.goodlist.splice(index, 1);
      // 2.删除本地数据
      this.$store.commit("delGoods", id);
    },
    selectedChanged(id,value){
      //switch值改变，相应改变本地存储中的selected属性
      // console.log(id+"---"+value);
    this.$store.commit("getSelectedValue",{id:id,value:value});
    }
  },
  components: {
    shopcar_number
  }
};
</script>

<style lang="scss" scoped>
.shopcar_container {
  background-color: #eeeeee;
  overflow: hidden;
  .mui-card-content-inner {
    display: flex;
    align-items: center;
    img {
      width: 60px;
      height: 60px;
    }
    .infos {
      margin-left: 10px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      h3 {
        font-size: 13px;
      }
      .li {
        .price {
          color: red;
          font-weight: bold;
        }
      }
    }
  }
  .jiesuan {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .left {
      .red {
        color: red;
        font-weight: bold;
        margin: 0 5px;
      }
    }
  }
}
</style>
